<?php
class User extends Model {
	function User(){
		parent::Model();
    }
    function insertnewuser($mahasiswaId,$linkConfirm){
    	$result= array();
    	
    	$queryCek = "SELECT mahasiswaId FROM user WHERE mahasiswaId='$mahasiswaId' LIMIT 1";
    	$queryCek = $this->db->query($queryCek);
    	
	    if($queryCek->result()){
			$result['ketersediaan'] = FALSE;
	    }else{
	    	$result['ketersediaan'] = TRUE;
	    	$query = "INSERT INTO user (mahasiswaId, linkConfirm,username,password,isActive) VALUES ('$mahasiswaId', '$linkConfirm','','','')";
    		$query = $this->db->query($query);
	    	    if($query){
					$result['status']='berhasil';
					$query = "SELECT id FROM user WHERE mahasiswaId='$mahasiswaId' LIMIT 1";
					$query = $this->db->query($query);
			    	foreach ($query->result() as $row){
			  			$result ['id'] = $row->id;
						}
		       	}else{
					$result['status']='gagal';
	 			}
	    	}
       return json_encode($result);
    }
    
   // input mahasiswaID ma link confirm klo cocok TRUE
    function isCocok($id,$linkConfirm){
    	$query = "SELECT * FROM user WHERE id='$id' AND linkConfirm='$linkConfirm' LIMIT 1";
    	$query = $this->db->query($query);
		    
     		if($query->result()){
				return TRUE;
	   		}else{
	    		return FALSE;
	    	}
	    
    }
    
    function isActive($id){
    	$query = "SELECT * FROM user WHERE id='$id' LIMIT 1";
    	$query = $this->db->query($query);
    		foreach ($query->result() as $row){
				return $row->isActive;
			}
    }
    
	function isRegister($id){
    	$query = "SELECT id FROM user WHERE id='$id' LIMIT 1";
    	$query = $this->db->query($query);
			if($query->result()){
				return TRUE;
	   		}else{
	    		return FALSE;
	    	}
    }  
    
    function usernameIsUsed(){
   		$result= array();
        $query = "SELECT * FROM user";
    	$query = $this->db->query($query);
    	$i=0;
    		foreach ($query->result() as $row){
  				$result[$i] = $row->username;
  				$i++;
			}
    	
        return $result;
    }
    function setRegLogDate($id){
    	$query = "SELECT * FROM users_detail WHERE user_id='$id' LIMIT 1";
    	$query = $this->db->query($query);
    	$date = date("Ymd");
    	if($query->num_rows() < 1){
    		$query = "INSERT INTO users_detail (user_id,reg_date) VALUES ('$id','$date')";
    		$query = $this->db->query($query);
    	}else{
    		$query = "UPDATE users_detail SET reg_date='$date' WHERE user_id='$id'";
    		$query = $this->db->query($query);
    	}
    	$this->setLogDate($id);
    }
    
    function setLogDate($id){
    	$date = date("Ymd");
    	$query = "UPDATE users_detail SET lastlogin_date='$date' WHERE user_id='$id'";
    	$query = $this->db->query($query);
    }
    
    function getUsername($id){
    	$return = FALSE;
    	$query = "SELECT * FROM user WHERE id='$id' LIMIT 1";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		foreach ($query->result() as $row){
				$return  = $row->username;
			}
    	}
    	return $return;
    }
    
    function isExistActive($username){
    	$return = FALSE;
    	$query = "SELECT * FROM user WHERE username='$username' AND isActive=1 LIMIT 1";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		$return = TRUE;
    	}
    	return $return;
    }
    
 	function getPassword($uid){
    	$return = FALSE;
    	$query = "SELECT * FROM user WHERE id='$uid' AND isActive=1 LIMIT 1";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		foreach ($query->result() as $row){
				$return  = $row->password;
			}
    	}
    	return $return;
    }
    
	function updatePassword($uid,$newPassword){
    	$query = "UPDATE user SET password='$newPassword' WHERE id='$uid'";
    	$query = $this->db->query($query);
    }
    
    function getAllUsers($offset=0,$limit=10){
    	$return = array();
    	$query = "SELECT * FROM user WHERE isActive=1 ORDER BY id DESC LIMIT $limit OFFSET $offset";
    	$query = $this->db->query($query);
   	 	if($query->num_rows() > 0){
   	 		foreach ($query->result() as $row){
				$return['id'] []  = $row->id